package com.hooper.iot.face.channelInitializer;


import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component(value = "serverChannelInitializer")
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Value("${server.READER_IDLE_TIME_SECONDS}")
    private int READER_IDLE_TIME_SECONDS;

    @Value("${server.WRITER_IDLE_TIME_SECONDS}")
    private int WRITER_IDLE_TIME_SECONDS;

    @Value("${server.ALL_IDLE_TIME_SECONDS}")
    private int ALL_IDLE_TIME_SECONDS;


    @Resource(name = "serverHeartHandler")
    private ChannelInboundHandlerAdapter serverHeartHandler;

    @Resource(name = "faceServerHandler")
    private ChannelInboundHandlerAdapter otherServerHandler;

    @Resource(name = "remoteHandler")
    private ChannelInboundHandlerAdapter remoteHandler;

    @Resource(name = "noticeEventHandler")
    private ChannelInboundHandlerAdapter noticeEventHandler;

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline p = socketChannel.pipeline();
        //检测空闲必须放在这里 因为pipeline是分顺序加载的
        p.addLast("idleStateHandler", new IdleStateHandler(READER_IDLE_TIME_SECONDS
                , WRITER_IDLE_TIME_SECONDS, ALL_IDLE_TIME_SECONDS, TimeUnit.SECONDS));
        //解码器必须放在前面，否则发数据收不到
//        p.addLast(new ProtobufVarint32FrameDecoder());//添加protobuff解码器
//        p.addLast(new ProtobufDecoder(MessageBase.getDefaultInstance()));//添加protobuff对应类解码器
//        p.addLast(new ProtobufVarint32LengthFieldPrepender());//protobuf的编码器 和上面对对应
//        p.addLast(new ProtobufEncoder());//protobuf的编码器
        p.addLast("http-codec", new HttpServerCodec());
        p.addLast("aggregator", new HttpObjectAggregator(65536));
        //自定义的hanlder
        p.addLast("serverHeartHandler", serverHeartHandler);
        p.addLast("faceServerHandler", otherServerHandler);
        p.addLast("remoteHandler", remoteHandler);
        p.addLast("noticeEventHandler", noticeEventHandler);
    }
}
